package linkedlistskills;

public class solution86 {

    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode partition(ListNode head, int x) {
        ListNode dummy1 = new ListNode(-1);
        ListNode dummy2 = new ListNode(-102);

        dummy2.next = head;
        ListNode p2 = dummy2;
        ListNode p1 = p2.next;

        ListNode p = dummy1;

        while(p1!=null){
            if (p1.val<x){
                p2.next = p1.next;
                p.next = p1;
                p=p.next;
                p1 = p1.next;
            } else {
              p2 = p1;
              p1 = p1.next;
            }
        }

        p.next = dummy2.next;

        return dummy1.next;
    }
}
